package com.ssmdemo.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ssmdemo.aop.bean.PermissionCode;
import com.ssmdemo.pojo.Emp;
import com.ssmdemo.pojo.RoleEntity;
import com.ssmdemo.service.EmpService;
import com.ssmdemo.service.MenuService;
import com.ssmdemo.service.RoleEmpService;
import com.ssmdemo.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/getAuthorization")
public class AuthorizationController {

    @Autowired
    RoleService roleService;
    @Autowired
    EmpService empService;
    @Autowired
    RoleEmpService roleEmpService;
    @Autowired
    MenuService menuService;

    @RequestMapping("/toAuthorizationList")
    @PermissionCode(value = "auth:page")
    public String toAuthorizationList(Model model){
        List<RoleEntity> roleEntityList=roleService.selectList(null);
        model.addAttribute("roleList",roleEntityList);
        return "authorization/authorizationPage";
    }

    @RequestMapping("/toAuthorizationMenuZtree/{roleId}")
    public String toAuthorizationMenuZtree(@PathVariable Integer roleId,Model model){
        if (roleId==null||roleId==0){
            roleId=1;
        }
        model.addAttribute("roleId",roleId);
        return "authorization/authorizationMenuZtree";
    }

    @RequestMapping("/getRoleEmp")
    public String getRoleType(Integer roleId, String name, Model model){

        EntityWrapper<Emp> empEntityWrapper=new EntityWrapper<Emp>();
        if (!StringUtils.isEmpty(name)){
            empEntityWrapper.like("username",name);
        }

        List<Emp> empList=empService.selectList(empEntityWrapper);
        model.addAttribute("empList",empList);
        model.addAttribute("name",name);
        model.addAttribute("roleId",roleId);

        return "authorization/roleEmpList";
//        return "redirect:toAuthorizationList";
    }

    @RequestMapping("/getAuthorizationUserRole")
    @ResponseBody
    public String getAuthorizationUserRole(Integer roleId, @RequestParam("empIdArray[]") List<Integer> emplist, Model model){


        roleEmpService.insertRoleEmp(roleId,emplist);

//        return "authorization/authorizationPage";
        return "redirect:toAuthorizationList";
    }

    @RequestMapping("/getAuthorizationMenuListByRoleId/{roleId}")
    @ResponseBody
    public List<Map<String,Object>> getAuthorizationMenuListByRoleId(@PathVariable Integer roleId){

        if (StringUtils.isEmpty(roleId)){
            roleId=1;
        }

        List<Map<String,Object>> mapList=menuService.getAuthorizationMenuListByRoleId(roleId);

        for (Map<String,Object> map:mapList){
            Object value=map.get("checked");
            if (StringUtils.isEmpty(value)){
                map.put("checked",false);
            }else {
                map.put("checked",true);
            }
        }
        return mapList;
//        return "redirect:toMenuList";
    }

}
